package protocol.JT905_2014;

import java.util.HashMap;

public class Handle905_2014JT00 implements IHandle905_2014JT {
    @Override
    public String handle(JT_905_2014Cmd cmd) {
        byte subOrder = cmd.subOrder();

        switch (subOrder){
            case 0x00:
                return handle0000(cmd);
            case 0x01:
                return handle0001(cmd);
            case 0x02:
                return handle0002(cmd);
            default:
                return "深标:未匹配的命令字"+(cmd.readOrder()+","+cmd.readSubOrder());
        }
    }

    private String handle0000(JT_905_2014Cmd cmd) {
        StringBuilder builder = new StringBuilder();

        if (cmd.leftSize() == 7){
            builder.append("ISU->智能顶灯:获取顶灯设备的运行状态");
            builder.append("\r\n");

            builder.append("ISU当前时间:"+cmd.readBCD(7));
            builder.append("\r\n");
        }else{
            builder.append("智能顶灯->ISU:ISU获取顶灯设备的运行状态,智能顶灯应答");
            builder.append("\r\n");

            builder.append("设备编号:"+cmd.readBCD(5));
            builder.append("\r\n");

            builder.append("设备硬件版本号:"+cmd.readBCD(1));
            builder.append("\r\n");

            builder.append("软件主版本号:"+cmd.readBCD(1));
            builder.append("\r\n");

            builder.append("软件次版本号:"+cmd.readBCD(1));
            builder.append("\r\n");

            HashMap<Integer,String> list = new HashMap<>();
            list.put(0,"设备正常");
            int type = cmd.readInt(1);
            builder.append("顶灯设备状态("+type+"):"+list.getOrDefault(type,"异常"));
            builder.append("\r\n");

            list.clear();
            list.put(0,"空车");
            list.put(1,"载客");
            list.put(2,"停运");
            list.put(3,"电召");
            list.put(4,"报警");
            list.put(5,"显示防伪密标");
            list.put(6,"换班");
            type = cmd.readInt(1);
            builder.append("顶灯显示状态("+type+"):"+list.getOrDefault(type,"未定义"));
            builder.append("\r\n");

            list.clear();
            list.put(0,"一般模式");
            list.put(1,"夜间模式");
            type = cmd.readInt(1);

            builder.append("是否进入夜间模式("+type+"):"+list.getOrDefault(type,"未定义"));
            builder.append("\r\n");

            builder.append("RFU:"+cmd.readBCD(5));
        }

        return builder.toString();
    }



    private String handle0001(JT_905_2014Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:终端通用应答");
        builder.append("\r\n");
        builder.append("应答流水号："+cmd.readBCD(2));
        builder.append("\r\n");
        builder.append("应答ID："+cmd.readBCD(2));
        builder.append("\r\n");

        int res = cmd.readInt(1);
        HashMap<Integer,String> list = new HashMap<>();
        list.put(0,"成功/确认");
        list.put(1,"失败");
        list.put(2,"消息有误");

        builder.append("结果："+list.getOrDefault(res,"未定义"));
        builder.append("\r\n");

        return builder.toString();
    }

    private String handle0002(JT_905_2014Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:心跳");
        builder.append("\r\n");

        return builder.toString();
    }
}
